package cn.yoyo.service;

import cn.yoyo.dao.*;
import cn.yoyo.entity.*;
import cn.yoyo.tools.MySession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class BlogServiceImpl implements BlogService{
    @Autowired
    BlogDao blogDao;
    @Autowired
    BlogTypeDao blogTypeDao;
    @Autowired
    ContentDao contentDao;
    @Autowired
    LikeDao likeDao;
    @Autowired
    FavouriteDao favouriteDao;


    @Override
    public List<Blog> findAllCanSee() {
        return blogDao.findAllCanSee();
    }

    @Override
    public List<Blog> findAllCantSee() {
        return blogDao.findAllCantSee();
    }

    @Override
    public List<Blog> findAll() {
        return blogDao.findAll();
    }

    @Override
    public List<Blog> findByType(String typeId) {
        return blogDao.findByType(typeId);
    }

    @Override
    public List<Blog> findMyBlogs(String userId) {
        return blogDao.findMyBlogs(userId);
    }

    @Override
    public Blog findById(String blogId) {
        Blog blog = blogDao.findById(blogId);
        if (MySession.getUser() instanceof User){
            Like like = likeDao.findById(blogId,MySession.getUserId());
            Favourite favourite = favouriteDao.findById(blogId,MySession.getUserId());
            if (like!=null){
                blog.setLikeId(like.getId());
            }
            if (favourite!=null){
                blog.setFavouriteId(favourite.getId());
            }
        }
        return blog;
    }

    @Override
    public List<Like> findMyLikes() {
        List<Like> list = likeDao.findMyLikes(MySession.getUserId());
        List lst = new ArrayList();
        for (Like arr : list){
            Blog blog = blogDao.findById(arr.getBlogId());
            Favourite favourite = favouriteDao.findById(blog.getId(),MySession.getUserId());
            Like like = likeDao.findById(blog.getId(),MySession.getUserId());
            if (like != null){
                blog.setLikeId(like.getId());
            }
            if (favourite != null){
                blog.setFavouriteId(favourite.getId());
            }
            lst.add(blog);
        }
        return lst;
    }

    @Override
    public List<Favourite> findMyFavourites() {
        List<Favourite> list = favouriteDao.findMyFavourites(MySession.getUserId());
        List lst = new ArrayList();
        for (Favourite arr : list){
            Blog blog = blogDao.findById(arr.getBlogId());
            Favourite favourite = favouriteDao.findById(blog.getId(),MySession.getUserId());
            Like like = likeDao.findById(blog.getId(),MySession.getUserId());
            if (favourite != null){
                blog.setFavouriteId(favourite.getId());
            }
            if (like != null){
                blog.setLikeId(like.getId());
            }
            lst.add(blog);
        }
        return lst;
    }

    @Override
    public void add(BlogContent blogContent) {
        // 博客整体添加完后把id给到blog
        Content content = contentDao.add(blogContent.getContent());
        Blog blog = blogContent.getBlog();
        blog.setContentId(content.getId());
        blogDao.add(blog);
    }

    @Override
    public void update(BlogContent blogContent) {
        Blog blog = blogContent.getBlog();
        blog.setContentId(blogContent.getContent().getId());
        blogDao.update(blog);
        contentDao.update(blogContent.getContent());
    }

    @Override
    public void isPass(String blogId) {
        // 博客审核通过时,对应类型sum+1
        blogDao.isPass(blogId);
        Blog blog = blogDao.findById(blogId);
        BlogType blogType = blogTypeDao.findById(blog.getTypeId());
        blogTypeDao.updateSum(blogType.getId(),blogType.getSum()+1);
    }

    @Override
    public void noPass(String blogId) {
        blogDao.noPass(blogId);
    }

    @Override
    public void delete(String blogId) {
        // 用户删除博客之后,博客对应的Content也作删除 博客对应类型的数量-1
        Blog blog = blogDao.findById(blogId);
        BlogType blogType = blogTypeDao.findById(blog.getTypeId());
        blogDao.delete(blogId);
        if (blogType.getSum()>0){
            blogTypeDao.updateSum(blogType.getId(),blogType.getSum()-1);
        }
        contentDao.deleteById(blog.getContentId());
    }
}
